﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{
    public partial class getDealership : System.Web.UI.Page
    {
        private Dealership getDeal = new Dealership();
        private string name;
        private static int id;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.IsInRole("Manager"))
            {
                btnEdit.Visible = false;
                btnEdit.Enabled = false;
                btnDelete.Visible = false;
                btnDelete.Enabled = false;
                btnSave.Visible = false;
                btnSave.Enabled = false;
            }
            if (!IsPostBack)
            {
                //name = Request.QueryString["name"];
                id = Convert.ToInt32(Request.QueryString["dealership"]);
                
                DatabaseHandler.connect();
                //getDeal = DatabaseHandler.getDealership(name);
                getDeal = DatabaseHandler.getDealershipbyID(id);

                id = getDeal.Dealership_ID;
                txtname.Text = getDeal.ContactInformation.FirstName;
                txtowner.Text = getDeal.Owner;
                txtphone.Text = getDeal.ContactInformation.Workphone;
                txtstreet.Text = getDeal.ContactInformation.Address;
                txtcity.Text = getDeal.ContactInformation.City;
                txtstate.Text = getDeal.ContactInformation.State;
                txtZip.Text = getDeal.ContactInformation.Zip;
                txtServices.Text = getDeal.Services;
            } 
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                lbltitle.Text = "Update Dealership";
                btnEdit.Visible = false;
                btnSave.Visible = true;
                allowWrite(false);
                changeTheme(true);
            }
            catch (Exception ex)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Dealership saveDeal = new Dealership();
                saveDeal.Dealership_ID = id;
                saveDeal.ContactInformation.FirstName = txtname.Text;
                saveDeal.Owner = txtowner.Text;
                saveDeal.ContactInformation.Workphone = txtphone.Text;
                saveDeal.ContactInformation.Address = txtstreet.Text;
                saveDeal.ContactInformation.City = txtcity.Text;
                saveDeal.ContactInformation.State = txtstate.Text;
                saveDeal.ContactInformation.Zip = txtZip.Text;
                saveDeal.Services = txtServices.Text;

                DatabaseHandler.connect();
                DatabaseHandler.updateDealership(saveDeal);
                btnSave.Visible = false;
                btnEdit.Visible = true;
                allowWrite(true);
                changeTheme(false);
                lbltitle.Text = "Dealership";
                ((Main)((SearchMP)this.Master).Master).writeStatus("Changes Saved", false);
            }
            catch (Exception ex)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
            }
        }

        private void allowWrite(bool read)
        {
            txtname.ReadOnly = read;
            txtowner.ReadOnly = read;
            txtphone.ReadOnly = read;
            txtstreet.ReadOnly = read;
            txtcity.ReadOnly = read;
            txtstate.ReadOnly = read;
            txtZip.ReadOnly = read;
            txtServices.ReadOnly = read;
        }

        private void changeTheme(bool update)
        {
            string cssclass;
            if (update)
                cssclass = "updateDealershiptxtbox";
            else
                cssclass = "Dealershiptxtbox";

            txtname.CssClass = cssclass;
            txtowner.CssClass = cssclass;
            txtphone.CssClass = cssclass;
            txtstreet.CssClass = cssclass;
            txtcity.CssClass = cssclass;
            txtstate.CssClass = cssclass;
            txtZip.CssClass = cssclass;
            txtServices.CssClass = cssclass;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus("You do not have access to remove dealership", true);
            }
            else
            {
                try
                {
                    DatabaseHandler.connect();
                    getDeal = DatabaseHandler.getDealershipbyID(id);
                    DatabaseHandler.removeDealership(getDeal);
                    ((Main)((SearchMP)this.Master).Master).writeStatus("Dealership Removed", false);
                    Response.Redirect("DealersSearch.aspx");
                    btnDelete.Visible = false;
                }
                catch (Exception ex)
                {
                    ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
                }
            }
        }
    }
}
